/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.sel1602.csp.Dao.impl;

import edu.fjut.sel1602.csp.Dao.PetStoreDao;
import edu.fjut.sel1602.csp.entity.PetOwner;
import edu.fjut.sel1602.csp.entity.PetStore;
import edu.fjut.sel1602.csp.util.BaseDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo
 */
public class PetStoreDaoOracleImpl implements PetStoreDao{

    @Override
    public int add(PetStore store) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement pst=con.prepareStatement("insert into petstore(NAME,PASSWORD,BALANCE) values(?,?,?)");
            pst.setString(1, store.getName());
            pst.setString(2, store.getPassword());
            pst.setInt(3, store.getBalance());
            r=pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public List<String> getStoreName() {
       List<String> list=new ArrayList<String>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select NAME from petstore");
            while(rs.next())
            {
                String name=rs.getString("NAME");
                list.add(name);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

    @Override
    public List<PetStore> getAllStore() {
        List<PetStore> list=new ArrayList<PetStore>();
        try {
            Connection con=BaseDao.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from petstore");
            while(rs.next())
            {
                PetStore store=new PetStore();
                store.setName(rs.getString("NAME"));
                store.setID(rs.getInt("ID"));
                store.setPassword(rs.getString("PASSWORD"));
                store.setBalance(rs.getInt("BALANCE"));
                list.add(store);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return list;
    }

   
    }

    
  
